package com.lifetime.controller;

import com.lifetime.Utils.GsonUtils;
import com.lifetime.model.User;
import com.lifetime.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RestController
public class LoginController {
    @Autowired
    LoginService loginservice;


    @RequestMapping("/login")
    @CrossOrigin
    /**
     * map通过前端接受信息
     * request是为了把个人信息放进Session作用域中
     */
    public Map Login(@RequestBody Map map, HttpServletRequest request) {
        User user = loginservice.selectLogin((String) map.get("username"));
        Map<String, Object> loginInfo = new HashMap<>();
        if (user == null) {
            loginInfo.put("status", 2);
            loginInfo.put("info", "no user");
            return loginInfo;
        } else if (((String) user.getPassword()).equals((String) map.get("password"))) {
            request.getSession().setAttribute("userId", user.getId());
            loginInfo.put("status", 1);
            loginInfo.put("info", "success");
            return loginInfo;
        }
        loginInfo.put("status", 3);
        loginInfo.put("info", "password error");
        return loginInfo;
    }


    @RequestMapping("forget")
    public String forget() {
        loginservice.sendMail("验证码","84526","2398216116@qq.com");//String title, String content, String email
        return 123 + "";
    }

/*    @PostMapping("test")
    @CrossOrigin
    public String test(@RequestBody Map map) {
    //public String test(@PathVariable Map map) {
        System.out.println("前端进来了");
        System.out.println((String) map.get("name"));
      //  System.out.println((String)name);
        return "ok";
    }*/

    @Autowired
    LoginService loginService=null;
    @RequestMapping("/jumpGetface")
    public String getface(){
        return "getface.html";
    }



    @RequestMapping("/searchface")
    @ResponseBody
    public   String searchface(@RequestBody @RequestParam(name = "imagebast64") StringBuffer imagebast64, Model model, HttpServletRequest request) throws IOException {

        System.out.println("=======================================");

        Map<String, Object> searchface = loginService.searchface(imagebast64);
        if(searchface==null||searchface.get("user_id")==null){
            System.out.println("我进来了");
            String flag="fail";
            String s = GsonUtils.toJson(flag);
            return s;
        }
        String user_id = searchface.get("user_id").toString();
        String score=searchface.get("score").toString().substring(0,2);
        int i = Integer.parseInt(score);
        if(i>80){
            model.addAttribute("userinf",user_id);
            HttpSession session = request.getSession();
            session.setAttribute("userinf",user_id);
            System.out.println("存入session");
        }


        System.out.println(searchface);
        String s = GsonUtils.toJson(searchface);
        return s;


    }
    @RequestMapping("/facesucceed")
    public String Faceloginsucceed(){
        System.out.println(1222222);
        return "succeed";
    }

}
